var app = getApp()
var myhost = app.globalData.myhost
Page({
  data: {
    book: {},
    write: {},
    reader:[],
    catelogs:[],
    comment:[],
    shoucang: 0,
    active: 0,
    pinglun: 0,
    readmore: {
      status: false,
      tip: '收起'
    },
    colNum: 0,
    colNumShow: true,
    activeNames: [],
    activeNames1: [],
  },
  Change(event) {
    this.setData({
      activeNames: event.detail,
    });
  },
  Change1(event) {
    this.setData({
      activeNames1: event.detail,
    });
  },
  onChange(event) {
    this.setData({
      active: event.detail
    });
  },
  toggle() {
    const status = this.data.readmore.status
    this.setData({
      readmore: {
        status: !status,
        tip: status ? '收起' : '查看更多'
      }
    })
  },
  onLoad(options) {
    wx.request({
      url: myhost + '/book/detail?bookId=' + options.bookid,
      success: (res) => {
        res.data.pic = myhost + res.data.pic
        this.setData({
          book: res.data
        })
        wx.request({
          url: myhost + '/writer/selectByPrimaryKey?id=' + res.data.writerId,
          success: (res) => {
            this.setData({
              write: res.data
            })
          }
        })
      }
    })
    wx.request({
      url: myhost + '/collect/collectOfBookId?bookId=' + options.bookid,
      success: (res) => {
        this.setData({
          shoucang: res.data.length
        })
      }
    })
    //获取评论
    wx.request({
      url: myhost + '/comment/commentOfBookId?bookId=' + options.bookid,
      success: (res) => {
        this.setData({
          pinglun: res.data.length,
          comment:res.data
        })
        // console.log(res.data)
        var list=[]
        for(var i =0;i<res.data.length;i++){
          wx.request({
            url: myhost+'/consumer/selectByPrimaryKey?id='+res.data[i].userId,
            success:(res)=>{
              res.data.avator=myhost+res.data.avator
              list.push(res.data)
            }
          })
        }
        setTimeout(() => {
          this.setData({
            reader:list
          })
          // console.log("reader",this.data.reader)
        }, 1000)
      }
    })
    //获取目录
    wx.request({
      url: myhost+'/directory/book/detailall?bookId='+options.bookid,
      success: (res) => {
        this.setData({
          catelogs: res.data
        })
      }
    })
  },
})